<template>
	<view class="minh-bg">
		<fa-nav-search ref="search" @methods="searchKey" :showBack="true" :placeholder="'请输入要查询的ID/名称'"></fa-nav-search>
		<view class="pos-fix fullwidth" style="z-index: 10;">
			<u-tabs :list="tabList" height="96" :is-scroll="false" font-size="28" bar-width="80" :current="current" @change="tabsChange"></u-tabs>
		</view>
		
		<view class="pt-5">
			<view class="p-2" v-show="current == 0"><fa-check-list :list="normalList" @methods="toDetail"></fa-check-list></view>
			<view class="p-2" v-show="current == 1"><fa-check-list :list="profitList" @methods="toDetail" desc="profit"></fa-check-list></view>
			<view class="p-2" v-show="current == 2"><fa-check-list :list="lossList" @methods="toDetail" desc="loss"></fa-check-list></view>
		</view>
	</view>
</template>

<script>
	import faNavSearch from '@/components/fa-nav-search/fa-nav-search.vue'
	import faCheckList from '@/components/fa-check-list/fa-check-list.vue'
	export default {
		components: {
			faNavSearch,
			faCheckList
		},
		data() {
			return {
				formData: {
					TaskGuid: undefined,
					Name: undefined,
					AssetType: undefined,
					Types: [], // 正常：ASSET_CHECK_TYPE_0001 , 盘亏：ASSET_CHECK_TYPE_0002 , 盘盈：ASSET_CHECK_TYPE_0003
				},
				tabList: [
					{name: '正常盘点', total: 0},
					{name: '盘盈明细', total: 0},
					{name: '盘亏明细', total: 0}
				],
				current: 0,
				normalList: [],
				profitList: [],
				lossList: [],
				cuOrgGuid:undefined,
				timeSearch: null
			}
		},
		onLoad(options) {
			// 盘点进入列表页的入口，激活对应的状态列表
			options.list == 'normal'   ? this.current = 0 : 
			options.list == 'increase' ? this.current = 1 : 
			options.list == 'lack' 	   ? this.current = 2 : null
			// 获取参数
			this.formData.TaskGuid = options.taskID;
			this.formData.AssetType = options.type;
			this.cuOrgGuid = options.cuOrgGuid;
			
			this.getCheckList(0); // 正常
			this.getCheckList(1); // 盘盈
			this.getCheckList(2); // 盘亏
		},
		methods: {
			// 获取列表数据
			getCheckList(status) {
				let listType = 
				status == 0 ? 'ASSET_CHECK_TYPE_0001' : 
				status == 1 ? 'ASSET_CHECK_TYPE_0003' : 
				status == 2 ? 'ASSET_CHECK_TYPE_0002' : null;
				this.formData.Types = [listType];
				this.$api.getCheckList(this.formData)
					.then(res => {
						if (res.code === 0) {
							this.tabList[status].total = res.data.length;
							switch (status){
								case 0:
									this.normalList = res.data;
									break;
								case 1:
									this.profitList = res.data;
									break;
								case 2:
									this.lossList 	= res.data;
									break;
							}
						} else {
							uni.showToast({
								title: res.message,
								icon: 'none'
							})
						}
					})
					.catch(err => console.error(err))
			},
			
			// 搜索框文字
			searchKey(msg) {
				// 开启函数防抖，避免快速输入搜索时数据异常
				if (this.timeSearch) {
					clearTimeout(this.timeSearch)
				}
				this.timeSearch = setTimeout(()=>{
					// 动作
					this.formData.Name = msg == "" ? undefined : msg;
					this.getCheckList(this.current);
					
					this.timeSearch = null; //清空定时器
				}, 200)
			},
			
			// tabs通知swiper切换
			tabsChange(index) {
				// this.formData.Name = "";
				// this.$refs['search'].reset();
				this.current = index;
				this.getCheckList(this.current);
			},
			
			// 页面跳转
			toDetail(data) {
				uni.navigateTo({
					url: `/pages/manage/itemAssetDetail?id=${data.AssetGuid}&current=${this.current}&cuOrgGuid=${this.cuOrgGuid}`
				})
			}
		}
	}
</script>

<style>
</style>
